home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Pascal / Book Demos in Pascal / ResourcesDemo / ResourcesDemo.p < prev    next >
Text File  |  1995-05-26  |  2KB  |  78 lines

  1. program ResourcesDemo;
  2.  
  3. (* ResourcesDemo *)
  4. (* by Ingemar Ragnemalm 1995 *)
  5.  
  6. (* Demonstrates how to use custom resources from your programs. *)
  7.  
  8. (* A demo program that has a resource, stored in itself (should rather be in a *)
  9. (* preference file in a real program) with a single variable, myCount. *)
  10. (* Each time the program is run, the resource is read, myCount is incremented, and *)
  11. (* the program beeps as many times as myCount says. *)
  12.  
  13. {$IFC UNDEFINED THINK_PASCAL}
  14.     uses Types, QuickDraw, Memory, Resources;
  15. {$ENDC}
  16.  
  17. (* Define a structure and a handle type to it. This is our resource format! *)
  18.  
  19.     type
  20.         MyRecord = record
  21.                 myCount: Integer;
  22.             end;
  23.         MyPtr = ^MyRecord;
  24.         MyHnd = ^MyPtr;
  25.  
  26.  
  27. (* Resource type and number. Use mixed-type names to avoid conflicts *)
  28.  
  29.     const
  30.         kMyResType = 'Demo';
  31.         kMyResNum = 0;
  32.  
  33.     var
  34.         myResource: MyHnd;    (* The handle to our resource *)
  35.  
  36.         i: Integer;            (* An integer for use as loop variable *)
  37.  
  38.  
  39.     procedure InitToolbox;
  40.     begin
  41. {$IFC UNDEFINED THINK_PASCAL}
  42.         InitGraf(@qd.thePort);
  43.         InitCursor;
  44.         MaxApplZone;
  45. {$ENDC}
  46.     end; (*InitToolbox*)
  47.  
  48.  
  49. begin
  50.     InitToolbox;
  51.  
  52. (**** Get the resource ****)
  53.  
  54.     myResource := MyHnd(GetResource(kMyResType, kMyResNum));
  55.  
  56. (**** Create the resource if needed ****)
  57.  
  58. (* If it didn't load, we assume it doesn't exist. Create it! *)
  59. (* This is done by allocating a handle, initializing the fields and calling AddResource. *)
  60.     if myResource = nil then
  61.         begin
  62.             myResource := MyHnd(NewHandle(sizeof(MyRecord)));            (* Allocates memory *)
  63.             myResource^^.myCount := 0;                                    (* Init fields *)
  64.             AddResource(Handle(myResource), kMyResType, kMyResNum, '');    (* Create resource *)
  65.         end;
  66.  
  67. (**** Change the resource ****)
  68.  
  69.     myResource^^.myCount := myResource^^.myCount + 1;        (* Change the count *)
  70. (* Call ChangedResource to tell MacOS that the resource needs to be written *)
  71. (* back to disk. *)
  72.     ChangedResource(Handle(myResource));
  73.  
  74. (**** Use the resource. Beep as many times as myCount says. ****)
  75.     for i := 1 to myResource^^.myCount do
  76.         SysBeep(1);
  77.  
  78. end. (* ResourcesDemo *)